package com.formiaomiao.demain.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.activity.DemainUserActivity;
import com.formiaomiao.demain.adapter.FollowAdapter;
import com.formiaomiao.demain.base.BaseFragment;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.model.FollowUFBean;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class FollowFansFragment extends BaseFragment implements FollowAdapter.onItemClickListener{

    @BindView(R.id.storeHouseHeader)
    MaterialHeader storeHouseHeader;
    @BindView(R.id.rc_follow)
    RecyclerView rcFollow;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;

    FollowUFBean followUFBean = new FollowUFBean();
    List<FollowUFBean.DataBean.ListBean> list = new ArrayList<>();
    FollowAdapter adapter;

    LoadingDialog dialog;
    LinearLayoutManager manager;

    private int page = 1;

    public static FollowFansFragment newInstance() {
        Bundle args = new Bundle();
        FollowFansFragment fragment = new FollowFansFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_follow_layout, container, false);
        unbinder = ButterKnife.bind(this, v);
        ViewInit();
        return v;
    }

    private void ViewInit() {
        dialog = new LoadingDialog(getActivity());
        manager = new LinearLayoutManager(getActivity());
        adapter = new FollowAdapter(getActivity(),list);
        rcFollow.setLayoutManager(manager);
        rcFollow.setAdapter(adapter);
        adapter.setOnItemckicklistener(this);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                DataInit(++page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                DataInit(page);
            }
        });
        rcFollow.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int lastVisiblePosition = manager.findLastVisibleItemPosition();
                    if (lastVisiblePosition >= manager.getItemCount() - 9) {
                        DataInit(++page);
                    }
                }
            }
        });

        dialog.show();
        DataInit(1);
    }

    private void DataInit(final int page) {
        HttpParams params = new HttpParams();
        params.put("id", StaticValues.userid);
        params.put("pageNum", page);
        params.put("pageSize", 20);
        OkGo.<String>post(BaseUrl.SELECTFANSFOLLOW)
                .headers("Authorization", PreferenceUtils.getString(getActivity(), PreferenceValues.TOKEN))
                .params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if(page==1){
                    list.clear();
                }
                followUFBean = gson.fromJson(response.body(),FollowUFBean.class);
                if(followUFBean.getCode()==200){
                    list.addAll(followUFBean.getData().getList());
                    adapter.notifyDataSetChanged();
                }else{
                    ToastUtils.show(getActivity(),followUFBean.getMsg());
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                dialog.dismiss();
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void itemckicklistener(View v, int i) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), DemainUserActivity.class);
        intent.putExtra("id", list.get(i).getId());
        intent.putExtra("name", list.get(i).getNickName());
        intent.putExtra("head", list.get(i).getUserHead());
        intent.putExtra("username", list.get(i).getUserName());
        startActivity(intent);
    }
}
